clear			all
set 			more off
cd 				"~/Dropbox/Indonesia Migration/" // Specify your root directory here
/*******************************************************************************
				Conventional shift-share
********************************************************************************/
foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year geolev2 native oldmig perwt 
label 			var geolev2 "Residence district at the time of the survey"
bys				geolev2: egen pop_it = total(perwt) // Resident population of each province
bys				geolev2: egen popnative_it = total(perwt*native) // Resident population of each province
bys				geolev2: egen popoldmig_it = total(perwt*oldmig) // Resident population of each province
foreach 		i in pop_it popnative_it popoldmig_it {
gen				ln`i'= ln(`i')
}
keep			lnpop_it lnpopnative_it lnpopoldmig_it year geolev2
duplicates		drop geolev2 year, force
tempfile		a_`t'_pop_ict			
save			`a_`t'_pop_ict' // origin-destination pair-level survey year files	
}
use				`a_1976_pop_ict', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `a_`t'_pop_ict'
}
merge			1:1 geolev2 year using "Data/Merged"
drop			_merge
drop 			if missing(lnpop_it,lnpopnative_it,lnpopoldmig_it)

label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_it "log of district population"
label 			var  lnpopnative_it "log population of natives"
label 			var  lnpopoldmig_it "log populationof old migrants"
				

quietly ivreg2	lnpopnative_it (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev2) first
eststo p1		

quietly ivreg2	lnpopoldmig_it (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev2) first
eststo p2


foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year migstat bpl origin perwt 
keep 			if migstat == 1
gen 			displaced_native = origin == bpl
gen 			displaced_oldmig = displaced_native==0
bys				origin: egen pop_displacednative = total(perwt*displaced_native)
bys				origin: egen pop_displacedoldmig = total(perwt*displaced_oldmig)
drop			migstat bpl perwt
rename			origin geolev1
duplicates		drop geolev1, force
tempfile		outflow_`t'
save			`outflow_`t''
use				"Data/Census_`t'.dta", clear
bys				geolev1: egen pop_it = total(perwt) // Resident population of each province
duplicates		drop geolev1, force
merge 1:1		geolev1	using `outflow_`t''
gen 			lnpop_displacednative = ln(pop_displacednative)
gen 			lnpop_displacedoldmig = ln(pop_displacedoldmig)
keep			lnpop_displacednative lnpop_displacedoldmig year geolev1
tempfile		logoutflow_`t'
save			`logoutflow_`t''
}
use				`logoutflow_1976', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `logoutflow_`t''
}
merge			1:m geolev1 year using "Data/Merged province level"
drop			_merge
drop 			if missing(lnpop_displacednative,lnpop_displacedoldmig)
label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_displacednative "log population share of natives who migrated out"
label 			var  lnpop_displacedoldmig "log population share of old migrants who migrated out"

				
quietly ivreg2	lnpop_displacednative (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
eststo p3

quietly ivreg2	lnpop_displacedoldmig (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
eststo p4	

coefplot (p1, keep(per_immig_cst_5Y) mcolor(black) offset(-2.5) ciopts(recast(rcap) lcolor(black))) ///
(p2, keep(per_immig_cst_5Y) mcolor(black) offset(-1.5) ciopts(recast(rcap) lcolor(black))) ///
(p3, keep(per_immig_cst_5Y) mcolor(black) offset(-0.5) ciopts(recast(rcap) lcolor(black))) ///
(p4, keep(per_immig_cst_5Y) mcolor(black) offset(0.5) ciopts(recast(rcap) lcolor(black))), ///
vertical yline(0, lpattern (dash) lcolor(red)) scheme(s1color) leg(off) /// 
xlabel(-1.5 `" "All" "Natives" "' -0.5 `" "All" "Old Migrants" "' 0.5 `" "Out-migrating" "Natives" "' 1.5 `" "Out-migrating" "Old Migrants" "', /// 
noticks labsize(small)) ytitle("Impact on population size and out-migration") /// 
title("{bf:Panel A:} Shift-share instrument using {it:t-5} migration settlement", size(small)) /// 
xsize(8) ysize(8) leg(off) /// 
graphregion(margin(2 4 2 2)) xscale(r(-2 2) noextend titlegap(2)) yscale(titlegap(2))

graph export "Coefplots/T3_pop_t5.jpg", as(jpg) name("Graph") quality(100) replace

/*******************************************************************************
				Shift-share with 10-year lag
********************************************************************************/
foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year geolev2 native oldmig perwt 
label 			var geolev2 "Residence district at the time of the survey"
bys				geolev2: egen pop_it = total(perwt) // Resident population of each province
bys				geolev2: egen popnative_it = total(perwt*native) // Resident population of each province
bys				geolev2: egen popoldmig_it = total(perwt*oldmig) // Resident population of each province
foreach 		i in pop_it popnative_it popoldmig_it {
gen				ln`i'= ln(`i')
}
keep			lnpop_it lnpopnative_it lnpopoldmig_it year geolev2
duplicates		drop geolev2 year, force
tempfile		a_`t'_pop_ict			
save			`a_`t'_pop_ict' // origin-destination pair-level survey year files	
}
use				`a_1976_pop_ict', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `a_`t'_pop_ict'
}
merge			1:1 geolev2 year using "Data/Merged"
drop			_merge
drop 			if missing(lnpop_it,lnpopnative_it,lnpopoldmig_it)

label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_it "log of district population"
label 			var  lnpopnative_it "log population of natives"
label 			var  lnpopoldmig_it "log populationof old migrants"


quietly ivreg2	lnpopnative_it (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev2) first
eststo p1		

quietly ivreg2	lnpopoldmig_it (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev2) first
eststo p2


foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year migstat bpl origin perwt 
keep 			if migstat == 1
gen 			displaced_native = origin == bpl
gen 			displaced_oldmig = displaced_native==0
bys				origin: egen pop_displacednative = total(perwt*displaced_native)
bys				origin: egen pop_displacedoldmig = total(perwt*displaced_oldmig)
drop			migstat bpl perwt
rename			origin geolev1
duplicates		drop geolev1, force
tempfile		outflow_`t'
save			`outflow_`t''
use				"Data/Census_`t'.dta", clear
bys				geolev1: egen pop_it = total(perwt) // Resident population of each province
duplicates		drop geolev1, force
merge 1:1		geolev1	using `outflow_`t''
gen 			lnpop_displacednative = ln(pop_displacednative)
gen 			lnpop_displacedoldmig = ln(pop_displacedoldmig)
keep			lnpop_displacednative lnpop_displacedoldmig year geolev1
tempfile		logoutflow_`t'
save			`logoutflow_`t''
}
use				`logoutflow_1976', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `logoutflow_`t''
}
merge			1:m geolev1 year using "Data/Merged province level"
drop			_merge
drop 			if missing(lnpop_displacednative,lnpop_displacedoldmig)
label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_displacednative "log population share of natives who migrated out"
label 			var  lnpop_displacedoldmig "log population share of old migrants who migrated out"

				
quietly ivreg2	lnpop_displacednative (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
eststo p3		

quietly ivreg2	lnpop_displacedoldmig (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
eststo p4				

coefplot (p1, keep(per_immig_cst_5Y) mcolor(black) offset(-2.5) ciopts(recast(rcap) lcolor(black))) ///
(p2, keep(per_immig_cst_5Y) mcolor(black) offset(-1.5) ciopts(recast(rcap) lcolor(black))) ///
(p3, keep(per_immig_cst_5Y) mcolor(black) offset(-0.5) ciopts(recast(rcap) lcolor(black))) ///
(p4, keep(per_immig_cst_5Y) mcolor(black) offset(0.5) ciopts(recast(rcap) lcolor(black))), ///
vertical yline(0, lpattern (dash) lcolor(red)) scheme(s1color) leg(off) /// 
xlabel(-1.5 `" "All" "Natives" "' -0.5 `" "All" "Old Migrants" "' 0.5 `" "Out-migrating" "Natives" "' 1.5 `" "Out-migrating" "Old Migrants" "', /// 
noticks labsize(small)) ytitle("Impact on population size and out-migration") /// 
title("{bf:Panel B:} Shift-share instrument using {it:t-10} migration settlement", size(small)) /// 
xsize(8) ysize(8) leg(off) /// 
graphregion(margin(2 4 2 2)) xscale(r(-2 2) noextend titlegap(2)) yscale(titlegap(2))

graph export "Coefplots/T3_pop_t10.jpg", as(jpg) name("Graph") quality(100) replace

/*******************************************************************************
				Multiple instruments
********************************************************************************/
foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year geolev2 native oldmig perwt 
label 			var geolev2 "Residence district at the time of the survey"
bys				geolev2: egen pop_it = total(perwt) // Resident population of each province
bys				geolev2: egen popnative_it = total(perwt*native) // Resident population of each province
bys				geolev2: egen popoldmig_it = total(perwt*oldmig) // Resident population of each province
foreach 		i in pop_it popnative_it popoldmig_it {
gen				ln`i'= ln(`i')
}
keep			lnpop_it lnpopnative_it lnpopoldmig_it year geolev2
duplicates		drop geolev2 year, force
tempfile		a_`t'_pop_ict			
save			`a_`t'_pop_ict' // origin-destination pair-level survey year files	
}
use				`a_1976_pop_ict', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `a_`t'_pop_ict'
}
merge			1:1 geolev2 year using "Data/Merged"
drop			_merge
drop 			if missing(lnpop_it,lnpopnative_it,lnpopoldmig_it)

label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_it "log of district population"
label 			var  lnpopnative_it "log population of natives"
label 			var  lnpopoldmig_it "log populationof old migrants"


quietly ivreg2	lnpopnative_it (per_immig_cst_5Y per_immig_cst_5Y_lag = ///
				pred_per_immig_cst_Jaeger_10Y pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct , cl(geolev2) first	
eststo p1	
				
quietly ivreg2	lnpopoldmig_it (per_immig_cst_5Y per_immig_cst_5Y_lag = ///
				pred_per_immig_cst_Jaeger_10Y pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct , cl(geolev2) first	
eststo p2				


foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year migstat bpl origin perwt 
keep 			if migstat == 1
gen 			displaced_native = origin == bpl
gen 			displaced_oldmig = displaced_native==0
bys				origin: egen pop_displacednative = total(perwt*displaced_native)
bys				origin: egen pop_displacedoldmig = total(perwt*displaced_oldmig)
drop			migstat bpl perwt
rename			origin geolev1
duplicates		drop geolev1, force
tempfile		outflow_`t'
save			`outflow_`t''
use				"Data/Census_`t'.dta", clear
bys				geolev1: egen pop_it = total(perwt) // Resident population of each province
duplicates		drop geolev1, force
merge 1:1		geolev1	using `outflow_`t''
gen 			lnpop_displacednative = ln(pop_displacednative)
gen 			lnpop_displacedoldmig = ln(pop_displacedoldmig)
keep			lnpop_displacednative lnpop_displacedoldmig year geolev1
tempfile		logoutflow_`t'
save			`logoutflow_`t''
}
use				`logoutflow_1976', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `logoutflow_`t''
}
merge			1:m geolev1 year using "Data/Merged province level"
drop			_merge
drop 			if missing(lnpop_displacednative,lnpop_displacedoldmig)
label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_displacednative "log population share of natives who migrated out"
label 			var  lnpop_displacedoldmig "log population share of old migrants who migrated out"


quietly ivreg2	lnpop_displacednative (per_immig_cst_5Y per_immig_cst_5Y_lag =  ///
				pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct av_age_ct, ///
				cl(geolev1) first	
eststo p3
			
quietly ivreg2	lnpop_displacedoldmig (per_immig_cst_5Y per_immig_cst_5Y_lag =  ///
				pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct av_age_ct, ///
				cl(geolev1) first
eststo p4

coefplot (p1, keep(per_immig_cst_5Y) mcolor(black) offset(-2.6) ciopts(recast(rcap) lcolor(black))) ///
(p1, keep(per_immig_cst_5Y_lag) msymbol(S) mcolor(navy) offset(-3.4) ciopts(recast(rcap) lcolor(navy))) ///
(p2, keep(per_immig_cst_5Y) mcolor(black) offset(-1.6) ciopts(recast(rcap) lcolor(black))) ///
(p2, keep(per_immig_cst_5Y_lag) msymbol(S) mcolor(navy) offset(-2.4) ciopts(recast(rcap) lcolor(navy))) ///
(p3, keep(per_immig_cst_5Y) mcolor(black) offset(-0.6) ciopts(recast(rcap) lcolor(black))) ///
(p3, keep(per_immig_cst_5Y_lag) msymbol(S) mcolor(navy) offset(-1.4) ciopts(recast(rcap) lcolor(navy))) ///
(p4, keep(per_immig_cst_5Y) mcolor(black) offset(0.6) ciopts(recast(rcap) lcolor(black))) ///
(p4, keep(per_immig_cst_5Y_lag) msymbol(S) mcolor(navy) offset(-0.2) ciopts(recast(rcap) lcolor(navy))), ///
vertical yline(0, lpattern (dash) lcolor(red)) scheme(s1color) /// 
xlabel(-1.5 `" "All" "Natives" "' -0.5 `" "All" "Old Migrants" "' 0.5 `" "Out-migrating" "Natives" "' 1.5 `" "Out-migrating" "Old Migrants" "', /// 
noticks labsize(small)) ytitle("Impact on population size and out-migration") xsize(12) ysize(8) /// 
title("{bf:Panel C:} Multiple instruments using {it:t-10} migration settlement", size(small)) ///
legend(order(2 "Share of new migrants in year {it:t}" 4 "Share of new migrants in year {it:t-5}")) /// 
graphregion(margin(2 4 2 2)) xscale(r(-2 2) noextend titlegap(2)) yscale(titlegap(2))

graph export "Coefplots/T3_pop_mt10.jpg", as(jpg) name("Graph") quality(100) replace				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
